package com.course.sysservers.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.http.requ.*;
import com.course.component.util.CopyUtil;
import com.course.component.util.TreeUtil;
import com.course.dto.ResponseArticle;
import com.course.dto.ResponseCategory;
import com.course.dto.ResponseNaves;
import com.course.dto.ResponseSwiper;
import com.course.entity.Articles;
import com.course.entity.Category;
import com.course.entity.Naves;
import com.course.entity.Swipers;
import com.course.service.*;
import com.course.sysservers.SystemServer;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/19 18:57
 * @Description: **********************
 */
@Service
public class SystemServerImpl implements SystemServer {
    @Resource
    private NavesService navesService;
    @Resource
    private SwipersService swipersService;
    @Resource
    private ArticlesService articlesService;
    @Resource
    private CategoryService categoryService;

    @Override
    public List<ResponseNaves> naveList(RequestNaves requestNaves) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("parent_id", requestNaves.getParentId());
        List<Naves> navesLists = navesService.list(wrapper);
        return TreeUtil.build(CopyUtil.copyList(navesLists, ResponseNaves.class));
    }

    @Override
    public List<ResponseSwiper> swiperList(RequestSwiper requestSwiper) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("isused", requestSwiper.getIsused());
        List<Swipers> swiperLists = swipersService.list(wrapper);
        return CopyUtil.copyList(swiperLists, ResponseSwiper.class);
    }

    @Override
    public List<ResponseArticle> artList(RequestSysArticles requestArticle) {
        QueryWrapper wrapper = new QueryWrapper();
        if (requestArticle.getIshot() != null) {
            wrapper.eq("ishot", requestArticle.getIshot());
        }
        if (requestArticle.getIsdel() != null) {
            wrapper.eq("isdel", requestArticle.getIsdel());
        }
        if (requestArticle.getIstop() != null) {
            wrapper.eq("istop", requestArticle.getIstop());
        }
        if (null != requestArticle.getCategoryId()) {
            wrapper.in("category_id", requestArticle.getCategoryId());
        }
        if (requestArticle.getLimit() > 0) {
            wrapper.last("limit " + requestArticle.getLimit());
        }

        return getResponseArticles(wrapper);
    }

    @NotNull
    private List<ResponseArticle> getResponseArticles(QueryWrapper wrapper) {
        List<Articles> articlesLists = articlesService.list(wrapper);
        List<ResponseArticle> responseArticles = CopyUtil.copyList(articlesLists, ResponseArticle.class);
        for (ResponseArticle item : responseArticles) {
            Category cat = categoryService.getById(item.getCategoryId());
            item.setCategory(CopyUtil.copy(cat, ResponseCategory.class));
        }
        return responseArticles;
    }

    @Override
    public Page<ResponseArticle> artPage(RequestSysArticles requestArticle) {
        QueryWrapper wrapper = new QueryWrapper();
        if (requestArticle.getIshot() != null) {
            wrapper.eq("ishot", requestArticle.getIshot());
        }
        if (requestArticle.getIsdel() != null) {
            wrapper.eq("isdel", requestArticle.getIsdel());
        }
        if (requestArticle.getIstop() != null) {
            wrapper.eq("istop", requestArticle.getIstop());
        }
        if (null != requestArticle.getCategoryId()) {
            wrapper.in("category_id", requestArticle.getCategoryId());
        }
        Page<Articles> pagArticles = articlesService.page(new Page<>(requestArticle.getPage(), requestArticle.getPageSize()), wrapper);
        List<ResponseArticle> listResponseArticle = CopyUtil.copyList(pagArticles.getRecords(), ResponseArticle.class);
        Page<ResponseArticle> res = new Page<>();
        res.setRecords(listResponseArticle);
        res.setTotal(pagArticles.getTotal());
        res.setCurrent(pagArticles.getCurrent());
        return res;
    }

    @Override
    public List<ResponseCategory> catList(RequestCategory requestCategory) {
        List<Category> categoryList = categoryService.list();
        return CopyUtil.copyList(categoryList, ResponseCategory.class);
    }

    @Override
    public List<ResponseSwiper> addslist(RequestSwiper requestSwiper) {
        List<Swipers> swiperLists = swipersService.list();
        return CopyUtil.copyList(swiperLists, ResponseSwiper.class);
    }

    @Override
    public ResponseArticle article(RequestSysArticles requestArticle) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!(requestArticle.getIshot() == null)) {
            wrapper.eq("ishot", requestArticle.getIshot());
        }
        if (!(requestArticle.getIsdel() == null)) {
            wrapper.eq("isdel", requestArticle.getIsdel());
        }
        if (!(requestArticle.getIstop() == null)) {
            wrapper.eq("istop", requestArticle.getIstop());
        }
        if (null != requestArticle.getCategoryId()) {
            wrapper.in("category_id", requestArticle.getCategoryId());
        }
        if (null != requestArticle.getTitle()) {
            wrapper.like("title", requestArticle.getTitle());
        }
        if (null != requestArticle.getId()) {
            wrapper.eq("id", requestArticle.getId());
        }
        Articles article = articlesService.getOne(wrapper);
        return CopyUtil.copy(article, ResponseArticle.class);
    }
}
